package org.aphelie.torg.ruler.internal.ui.editor.character.actions;

import java.lang.reflect.InvocationTargetException;

import org.aphelie.torg.ruler.internal.ui.TGRPlugin;
import org.aphelie.torg.ruler.internal.ui.TGRUIMessages;
import org.eclipse.jface.action.Action;
import org.eclipse.swt.widgets.Event;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.texteditor.ITextEditor;

public class FormatAction extends Action {

	protected ITextEditor fTextEditor;

	public FormatAction() {
		setText(TGRUIMessages.FormatManifestAction_actionText);
	}

	public void runWithEvent(Event event) {
		run();
	}

	public void run() {
		if (fTextEditor == null || fTextEditor.getEditorInput() == null)
			return;

		try {
			PlatformUI.getWorkbench().getProgressService().busyCursorWhile(new FormatOperation(new Object[] {fTextEditor.getEditorInput()}));
		} catch (InvocationTargetException e) {
			TGRPlugin.log(e);
		} catch (InterruptedException e) {
			TGRPlugin.log(e);
		}
	}

	public void setTextEditor(ITextEditor textEditor) {
		// TODO Temporary:  Until plug-in manifest XML source page format
		// functionality is completed
		//TODO setEnabled(textEditor instanceof BundleSourcePage);
		fTextEditor = textEditor;
	}

}
